package com.blackfish.common.plugin;

import com.blackfish.common.config.Conf;
import com.blackfish.common.plugin.aio.HelloClientAioHandler;
import com.blackfish.common.plugin.aio.HelloPacket;
import com.blackfish.common.plugin.aio.client.ShowcaseClientAioHandler;
import com.blackfish.common.plugin.aio.client.ShowcaseClientAioListener;
import com.blackfish.common.plugin.aio.common.Const;
import com.blackfish.common.plugin.aio.common.ShowcasePacket;
import com.blackfish.common.plugin.aio.common.Type;
import com.blackfish.common.plugin.aio.common.packets.GroupMsgReqBody;
import com.blackfish.common.plugin.aio.common.packets.JoinGroupReqBody;
import com.blackfish.common.plugin.aio.common.packets.LoginReqBody;
import com.blackfish.common.plugin.aio.common.packets.P2PReqBody;
import org.tio.client.ClientChannelContext;
import org.tio.client.ClientGroupContext;
import org.tio.client.ReconnConf;
import org.tio.client.TioClient;
import org.tio.client.intf.ClientAioHandler;
import org.tio.client.intf.ClientAioListener;
import org.tio.core.Node;
import org.tio.core.Tio;
import org.tio.utils.hutool.StrUtil;
import org.tio.utils.json.Json;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class TioLoopClient{

    private static Node serverNode = null;

    //用来自动连接的，不想自动连接请设为null
    private static ReconnConf reconnConf = new ReconnConf(5000L);

    private static ClientAioHandler tioClientHandler = new ShowcaseClientAioHandler();
    private static ClientAioListener aioListener = new ShowcaseClientAioListener();
    private static ClientGroupContext clientGroupContext = new ClientGroupContext(tioClientHandler, aioListener, reconnConf);

    private static TioClient tioClient = null;

    static ClientChannelContext clientChannelContext;

    /**
     * 启动程序入口
     */
    public boolean start(String IP,int PORT) {
        serverNode = new Node(IP,PORT);
        clientGroupContext.setHeartbeatTimeout(Conf.TIMEOUT);
        boolean isConnect = false;
        try {
            tioClient = new TioClient(clientGroupContext);
            clientChannelContext = tioClient.connect(serverNode);
            isConnect = true;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return isConnect;
    }

    public static boolean send(String common,String userId,String content) throws Exception{
        boolean isSend = false;
        try {
            String line = String.join(" ",new String[]{common,userId,content});
            isSend = processCommand(line);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return isSend;
    }

    public static boolean processCommand(String line) throws Exception {
        if (StrUtil.isBlank(line)) {
            return false;
        }

        String[] args = line.split(" ");//StrUtil.split(line, " ");
        String command = args[0];

        if ("login".equalsIgnoreCase(command)) {
            String loginname = args[1];
            String password = args[2];

            LoginReqBody loginReqBody = new LoginReqBody();
            loginReqBody.setLoginname(loginname);
            loginReqBody.setPassword(password);

            ShowcasePacket reqPacket = new ShowcasePacket();
            reqPacket.setType(Type.LOGIN_REQ);
            reqPacket.setBody(Json.toJson(loginReqBody).getBytes(ShowcasePacket.CHARSET));

            Tio.send(clientChannelContext, reqPacket);
            return true;
        } else if ("join".equals(command)) {
            String group = args[1];

            JoinGroupReqBody joinGroupReqBody = new JoinGroupReqBody();
            joinGroupReqBody.setGroup(group);

            ShowcasePacket reqPacket = new ShowcasePacket();
            reqPacket.setType(Type.JOIN_GROUP_REQ);
            reqPacket.setBody(Json.toJson(joinGroupReqBody).getBytes(ShowcasePacket.CHARSET));

            Tio.send(clientChannelContext, reqPacket);
            return true;
        } else if ("groupMsg".equals(command)) {
            String group = args[1];
            String text = args[2];

            GroupMsgReqBody groupMsgReqBody = new GroupMsgReqBody();
            groupMsgReqBody.setToGroup(group);
            groupMsgReqBody.setText(text);

            ShowcasePacket reqPacket = new ShowcasePacket();
            reqPacket.setType(Type.GROUP_MSG_REQ);
            reqPacket.setBody(Json.toJson(groupMsgReqBody).getBytes(ShowcasePacket.CHARSET));

            Tio.send(clientChannelContext, reqPacket);
            return true;
        } else if ("p2pMsg".equals(command)) {
            String toUserid = args[1];
            String text = args[2];

            P2PReqBody p2pReqBody = new P2PReqBody();
            p2pReqBody.setToUserid(toUserid);
            p2pReqBody.setText(text);

            ShowcasePacket reqPacket = new ShowcasePacket();
            reqPacket.setType(Type.P2P_REQ);
            reqPacket.setBody(Json.toJson(p2pReqBody).getBytes(ShowcasePacket.CHARSET));

            Tio.send(clientChannelContext, reqPacket);
            return true;
        }
        return false;
    }
}
